using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Business.Entities.Manifest;

namespace OR.Lazarus.Business.Entities.Manifest.Lazarus
{
	/// <summary>
	/// 
	/// </summary>
   public class ManifestDetail : EntityBase
   {
		protected System.String mName;
		protected string mOutputPath;
		protected string mProjectName;
		protected string mDescription;
	
		protected Derived.DataSourceDetail mDataSource;
	
		protected Derived.FileCollection mFiles;

		public ManifestDetail()
		{
			mFiles = new Derived.FileCollection();
			mFiles.ParentEntity = this;
	
			mDataSource = new Derived.DataSourceDetail();
			mDataSource.ParentEntity = this;
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string OutputPath
		{
			get
			{
				return ( mOutputPath );
			}
			set
			{
				mOutputPath = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string ProjectName
		{
			get
			{
				return ( mProjectName );
			}
			set
			{
				mProjectName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Derived.DataSourceDetail DataSource
		{
			get
			{
				return ( mDataSource );
			}
			set
			{
				mDataSource = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.FileCollection Files
		{
			get
			{
				return ( mFiles );
			}
			set
			{
				mFiles = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mOutputPath = DataUtility.GetString( pSource, "output-path" );
			mProjectName = DataUtility.GetString( pSource, "project-name" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lm:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			

			// FK Values
			XmlElement vdatasource = ( XmlElement )pSource.SelectSingleNode( "lm:DataSource", PersistenceConfig.Namespaces );
			
			if ( vdatasource != null )
			{
				mDataSource.LoadXml( vdatasource );
			}
			XmlElement vFiles = ( XmlElement )pSource.SelectSingleNode( "lm:files", PersistenceConfig.Namespaces );
			
			if ( vFiles != null )
			{
				mFiles.LoadXml( vFiles );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lm", "manifest", "http://schemas.icitadel.com/Lazarus/Manifest.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mOutputPath ) )
			{
				vNewElement.SetAttribute( "output-path", mOutputPath );
			}
					
			if ( !String.IsNullOrEmpty( mProjectName ) )
			{
				vNewElement.SetAttribute( "project-name", mProjectName );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lm", "description", "http://schemas.icitadel.com/Lazarus/Manifest.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// FK Values
			vNewElement.AppendChild( mDataSource.SaveXml( p_docContext ) );
			

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mFiles.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.ManifestDetail v_oReturn = new Derived.ManifestDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.OutputPath = mOutputPath;
			v_oReturn.ProjectName = mProjectName;
			v_oReturn.Description = mDescription;
			v_oReturn.DataSource = ( Derived.DataSourceDetail )mDataSource.Clone();
			
			// FK Values
			v_oReturn.DataSource = this.DataSource.CloneTyped();
			

			// Collections

			foreach ( Derived.FileDetail vDetail in mFiles )
			{
				v_oReturn.Files.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.ManifestDetail CloneTyped()
		{
			return ( ( Derived.ManifestDetail )this.Clone() );
		}
   }
}
   